﻿
using CLCommuModbus;
using CLVisionCommuInterface;
using IniParser;
using IniParser.Model;
using NationalInstruments.TestStand.Interop.API;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace CLVisionCommu
{
    public partial class CLVisionCommuCaller
    {
        static Dictionary<int, ICLVisionCommu> CommuInstanceDict = new Dictionary<int, ICLVisionCommu>();

        public static bool Init(int threadCount, PropertyObject arrayContainerToReceiveConfig, out int ErrorCode, out string ErrorText, out bool Occurred)
        {
            ErrorCode = 0;
            ErrorText = "NoError";
            Occurred = false;
            try
            {
                //获取当前dll所在路径
                Type t = typeof(CLVisionCommuCaller);
                string currentdllPath = t.Assembly.Location;
                string currentDirctory = Path.GetDirectoryName(currentdllPath);
                string configFilePath = Path.Combine(currentDirctory, "CLVisionCommuPar.ini");

                for (int threadIndex = 0; threadIndex < threadCount; threadIndex++)
                {
                    //创建实例
                    ICLVisionCommu CommuInstance = CreateInstance(threadIndex, configFilePath, out ErrorCode, out ErrorText, out Occurred);
                    if (ErrorCode == -1) { Occurred = true; return false; }
                    //创建通讯
                    CommuInstance.Init(threadIndex, configFilePath, out ErrorCode, out ErrorText);
                    if (ErrorCode == -1) { Occurred = true; return false; }
                    CommuInstanceDict.Add(threadIndex, CommuInstance);
                }
                string objectConfigFilePath = Path.Combine(currentDirctory, "ObjecthandShakeMode2.cdat");

                if (arrayContainerToReceiveConfig != null)
                {
                    CreateObjectStructInTS(threadCount, arrayContainerToReceiveConfig, objectConfigFilePath, out ErrorCode, out ErrorText);
                    if (ErrorCode == -1) { Occurred = true; return false; }
                }
                return true;
            }
            catch (Exception ex)
            {
                Occurred = true;
                ErrorCode = -1;
                ErrorText = "初始化失败！" + ex.Message;
                return false;
            }
        }
        public static ICLVisionCommu CreateInstance(int threadIndex, string configFilePath, out int ErrorCode, out string ErrorText, out bool Occurred)
        {
            ErrorCode = 0;
            ErrorText = "NoError";
            Occurred = false; try
            {
                ICLVisionCommu cLVisionCommu = null;
                // string configFilePath = @"C:\Users\hao.chen\Desktop\CLVisionCommuPar.ini";
                string interfaceDllName = ReadIniFile(configFilePath, "Thread" + threadIndex.ToString(), "InterfaceDLL", out ErrorCode, out ErrorText);
                if (ErrorCode == -1) { Occurred = true; return null; }

                string currentDirctory = Path.GetDirectoryName(configFilePath);
                //反射加载dll
                Assembly assembly = Assembly.LoadFrom(Path.Combine(currentDirctory, interfaceDllName));
                //获取程序集中的类型
                Type[] mtypes = assembly.GetTypes();
                var type = mtypes.Where(x => x.Name == "CommuCaller").ToList();
                // Type type = assembly.GetType("CommuCaller");
                //创建实例
                object commuInstance = Activator.CreateInstance(type[0]);

                cLVisionCommu = commuInstance as ICLVisionCommu;
                return cLVisionCommu;
            }
            catch (Exception ex)
            {
                Occurred = true;
                ErrorCode = -1;
                ErrorText = "创建通讯失败！" + ex.Message;
                return null;
            }
        }
        public static bool SendConstantData(int threadIndex, string constantName, double constantValue, out int ErrorCode, out string ErrorText, out bool Occurred)
        {
            ErrorCode = 0;
            ErrorText = "NoError";
            Occurred = false;
            try
            {
                bool result = CommuInstanceDict[threadIndex].SendConstantData(constantName, constantValue, out ErrorCode, out ErrorText);
                if (ErrorCode == -1) { Occurred = true; return false; }
                return result;
            }
            catch (Exception ex)
            {
                Occurred = true;
                ErrorCode = -1;
                ErrorText = "发送固定地址数据失败！" + ex.Message;
                return false;
            }
        }

        public static bool SendObjectData(int threadIndex, int objectID, PropertyObject containerToSendValue, out int ErrorCode, out string ErrorText, out bool Occurred)
        {
            ErrorCode = 0;
            ErrorText = "NoError";
            Occurred = false;
            try
            {
                bool result = CommuInstanceDict[threadIndex].SendObjectData(objectID, containerToSendValue, out ErrorCode, out ErrorText);
                if (ErrorCode == -1) { Occurred = true; return false; }
                return result;
            }
            catch (Exception ex)
            {
                Occurred = true;
                ErrorCode = -1;
                ErrorText = "发送Object数据失败！" + ex.Message;
                return false;
            }
        }

        public static bool ReceiveConstantData(int threadIndex, string constantName, out double value, out int ErrorCode, out string ErrorText, out bool Occurred)
        {
            ErrorCode = 0;
            ErrorText = "NoError";
            Occurred = false;
            value = -1;
            try
            {
                bool result = CommuInstanceDict[threadIndex].ReceiveConstantData(constantName, out value, out ErrorCode, out ErrorText);
                if (ErrorCode == -1) { Occurred = true; return false; }
                return result;
            }
            catch (Exception ex)
            {
                Occurred = true;
                ErrorCode = -1;
                ErrorText = "接收固定地址数据失败！" + ex.Message;
                return false;
            }
        }

        public static bool ReceiveObjectData(int threadIndex, int objectID, PropertyObject containerToReceiveValue, out int ErrorCode, out string ErrorText, out bool Occurred)
        {
            ErrorCode = 0;
            ErrorText = "NoError";
            Occurred = false;
            try
            {
                bool result = CommuInstanceDict[threadIndex].ReceiveObjectData(objectID, containerToReceiveValue, out ErrorCode, out ErrorText);
                if (ErrorCode == -1) { Occurred = true; return false; }
                return result;
            }
            catch (Exception ex)
            {
                Occurred = true;
                ErrorCode = -1;
                ErrorText = "接收Object数据失败！" + ex.Message;
                return false;
            }
        }




    }
}
